package com.leha.forum.lehare.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leha.forum.lehare.dto.PostEditDTO;
import com.leha.forum.lehare.entity.Post;
import com.leha.forum.lehare.service.PostService;
import com.leha.forum.lehare.mapper.PostMapper;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.function.Supplier;

/**
* @author Administrator
* @description 针对表【post】的数据库操作Service实现
* @createDate 2025-04-15 21:44:28
*/
@Service
public class PostServiceImpl extends ServiceImpl<PostMapper, Post>
    implements PostService{

    /**
     * 发布新帖子
     *
     * @param userId 用户ID
     * @param dto    帖子数据
     * @return 是否发布成功
     */
    @Override
    public boolean publishNewPost(Long userId, PostEditDTO dto) {
        if (userId <= 0) {
            throw new IllegalArgumentException("用户 ID 无效");
        }

        if (dto.getPostId() != null && dto.getPostId() <= 0) {
            throw new IllegalArgumentException("帖子 ID 无效");
        }

        if ("".equals(dto.getTitle())) {
            throw new IllegalArgumentException("帖子标题不能为空");
        }

        if ("".equals(dto.getContent())) {
            throw new IllegalArgumentException("帖子内容不能为空");
        }

        if (dto.getCategory() <= 0) {
            throw new IllegalArgumentException("帖子分类 ID 无效");
        }

        Post post = null;
        boolean saveOrUpdate;
        if (dto.getPostId() != null && dto.getPostId() > 0) {
            saveOrUpdate = false;
            post = getById(dto.getPostId());
            if (post == null) {
                throw new IllegalStateException("帖子" + dto.getPostId() + "不存在");
            }
        } else {
            saveOrUpdate = true;
            post = new Post();
            post.setPostId(null);

            post.setIsTop(0);
            post.setIsStar(0);
            post.setViewCount(0);
            post.setAttachments("");
            post.setCreatedAt(new Date());
        }

        post.setUserId(userId);
        post.setCategoryId(dto.getCategory());
        post.setTitle(dto.getTitle());
        post.setContent(dto.getContent());

        if (saveOrUpdate) {
            return save(post);
        } else {
            return updateById(post);
        }
    }

    @Override
    public IPage<Post> getPosts(Long page, Long size, Supplier<QueryWrapper<Post>> queryWrapperSupplier) {
        if (page <= 0) {
            throw new IllegalArgumentException("");
        }

        if (size <= 0) {
            throw new IllegalArgumentException("分页大小不能小于等于 0");
        }

        Page<Post> p = new Page<>(page, size);
        return page(p, queryWrapperSupplier.get());
    }

    /**
     * 设置置顶帖子
     *
     * @param postId 帖子ID
     * @param value  Boolean
     * @return 操作结果
     */
    @Override
    public boolean setTop(Long postId, Boolean value) {
        if (postId <= 0) {
            throw new IllegalArgumentException("帖子 ID 无效");
        }

        Post post = getById(postId);
        if (post == null) {
            throw new IllegalStateException("帖子 " + postId + " 不存在");
        }

        post.setIsTop(value ? 1 : 0);

        return updateById(post);
    }

    /**
     * 设置精华帖子
     *
     * @param postId 帖子ID
     * @param value  Boolean
     * @return 操作结果
     */
    @Override
    public boolean setStar(Long postId, Boolean value) {
        if (postId <= 0) {
            throw new IllegalArgumentException("帖子 ID 无效");
        }

        Post post = getById(postId);
        if (post == null) {
            throw new IllegalStateException("帖子 " + postId + " 不存在");
        }

        post.setIsStar(value ? 1 : 0);

        return updateById(post);
    }

    /**
     * 帖子阅读量加一
     *
     * @param postId 帖子ID
     * @return 操作结果
     */
    @Override
    public boolean increaseViewCount(Long postId) {
        if (postId <= 0) {
            throw new IllegalArgumentException("帖子 ID 无效");
        }

        Post post = getById(postId);
        if (post == null) {
            throw new IllegalStateException("帖子 " + postId + " 不存在");
        }

        post.setViewCount(post.getViewCount() + 1);

        return updateById(post);
    }
}




